package com.qf.flash.ui.home.adapter;

import android.graphics.Color;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.qf.dblib.dao.impl.UserMgr;
import com.qf.flash.R;
import com.qf.flash.constant.FlashCodes;
import com.qf.flash.model.MyLaunchBean;
import com.qf.flash.model.PayResetBean;
import com.qf.flash.ui.home.OrderPayActivity;
import com.qf.flash.ui.mine.GameDetailsActivity;
import com.qf.flashlib.adapter.BaseRViewItem;
import com.qf.flashlib.adapter.RViewHolder;
import com.qf.flashlib.util.SpannableStringUtil;

import static com.qf.flash.constant.Constant.INVITER;
import static com.qf.flash.constant.Constant.PINGJIA;

public class MyLaunchItem extends BaseRViewItem<Object> {
    @Override
    public boolean isItemView(Object entity, int position) {
        return super.isItemView(entity, position);
    }

    private int type;

    public MyLaunchItem(int type) {
        this.type = type;
    }

    @Override
    public int getItemLayout() {
        return R.layout.item_my_launch;
    }

    @Override
    public void convert(RViewHolder holder, Object entity, int position, int itemSize) {
        MyLaunchBean myLaunchBean = (MyLaunchBean) entity;

        ImageView iv = holder.getView(R.id.iv_img);
        holder.setText(R.id.tv_situation, myLaunchBean.getOrderTip());
        holder.setText(R.id.tv_name, myLaunchBean.getTeamName());
        holder.setText(R.id.tv_type, myLaunchBean.getTypeName());
        holder.setText(R.id.tv_time, myLaunchBean.getGameTime());
        if (!TextUtils.isEmpty(myLaunchBean.getShop().getShopName()))
            holder.setText(R.id.tv_bus_name, myLaunchBean.getShop().getShopName());
        if (!TextUtils.isEmpty(myLaunchBean.getShop().getLocation()))
            holder.setText(R.id.tv_address, myLaunchBean.getShop().getLocation());
        String str = myLaunchBean.getPayedNum() + " / " + myLaunchBean.getNeedNum();
        SpannableStringBuilder stringBuilder = SpannableStringUtil.changeTextColor(str, Color.parseColor("#FABE00"), 0, 1);
        holder.setText(R.id.tv_count, stringBuilder);
        holder.setText(R.id.tv_group_no, myLaunchBean.getOrderNumber());
        holder.setText(R.id.tv_nick_name, myLaunchBean.getCaptainName());


        Glide.with(holder.getmConvertView().getContext()).load(myLaunchBean.getPlayImage()).apply(new RequestOptions().placeholder(R.mipmap.picture)).into(iv);


        String orderState = myLaunchBean.getOrderState();

        int isSetRest = myLaunchBean.getIsSetRest();
        int isRestMoney = myLaunchBean.getIsRestMoney();
        double restFee = myLaunchBean.getRestFee();
        int responsePlay = myLaunchBean.getResponsePlay();
        int responseShop = myLaunchBean.getResponseShop();
        String isFree = myLaunchBean.getIsFree();
        if (!isFree.equals("1") && isSetRest == 1 && isRestMoney == 0 && restFee > 0) {
            holder.setVisible(R.id.tv_last, true);
            holder.setText(R.id.tv_last, "尾款￥" + String.valueOf(restFee));
        } else if (responsePlay == 1 && responseShop == 1) {
            holder.setVisible(R.id.tv_last, true);
            holder.setText(R.id.tv_last, "已评价");
        } else if (TextUtils.isEmpty(myLaunchBean.getPlayId()) && responseShop == 1) {
            holder.setVisible(R.id.tv_last, true);
            holder.setText(R.id.tv_last, "已评价");
        }else if (orderState.equals("4") && (responsePlay == 0 || responseShop == 0)) {
            holder.setVisible(R.id.tv_last, true);
            holder.setText(R.id.tv_last, "去写评价");
        }
        else {
            holder.setVisible(R.id.tv_last, false);
        }
//        if(type==2){
        holder.itemView.setOnClickListener(view -> {
            if (orderState.equals(FlashCodes.ORDER_STATE_YWC)) {
                GameDetailsActivity.start(holder.getmConvertView().getContext(), myLaunchBean.getId(), myLaunchBean.getPlayId(), PINGJIA, myLaunchBean.getPlayType());
            } else if (orderState.equals("1")) {
                GameDetailsActivity.start(holder.getmConvertView().getContext(), myLaunchBean.getId(), myLaunchBean.getPlayId(), INVITER, myLaunchBean.getPlayType());
            } else {
                GameDetailsActivity.start(holder.getmConvertView().getContext(), myLaunchBean.getId(), myLaunchBean.getPlayId(), "", myLaunchBean.getPlayType());
            }
        });
//        }

        holder.getView(R.id.tv_last).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isFree.equals("1") && isSetRest == 1 && isRestMoney == 0 && restFee > 0) {
                    PayResetBean payResetBean = new PayResetBean();
                    payResetBean.setOrderId(myLaunchBean.getId());
                    payResetBean.setUserId(UserMgr.getInstance().getLoginUser().getUid());
                    payResetBean.setMoney(restFee);
                    OrderPayActivity.start(holder.getmConvertView().getContext(), payResetBean);
                } else if (orderState.equals(FlashCodes.ORDER_STATE_YWC)) {
                    GameDetailsActivity.start(holder.getmConvertView().getContext(), myLaunchBean.getId(), myLaunchBean.getPlayId(), PINGJIA, myLaunchBean.getPlayType());
                } else if (orderState.equals("1")) {
                    GameDetailsActivity.start(holder.getmConvertView().getContext(), myLaunchBean.getId(), myLaunchBean.getPlayId(), INVITER, myLaunchBean.getPlayType());
                } else {
                    GameDetailsActivity.start(holder.getmConvertView().getContext(), myLaunchBean.getId(), myLaunchBean.getPlayId(), "", myLaunchBean.getPlayType());
                }
            }
        });

    }
}
